from dataclasses import dataclass
from enum import Enum
from typing import Optional, List, Dict, Any


class Subject(Enum):
    CHINESE = "语文"
    MATH = "数学"
    ENGLISH = "英语"


@dataclass
class Col:
    # 学科
    subject: Subject
    # 考试名称
    exam_title: str
    # 子测验名称，如数学A、数学B
    sub_exam_title: Optional[str] = None
    # 得分
    exam_score: Optional[float] = None

    def to_dict(self) -> Dict[str, Any]:
        return {
            'subject': self.subject.value,
            'exam_title': self.exam_title,
            'sub_exam_title': self.sub_exam_title,
            'exam_score': self.exam_score
        }


@dataclass
class Row:
    # 学号
    student_code: str
    # 学生姓名
    student_name: str
    # 成绩列表
    cols: List[Col]

    def to_dict(self) -> Dict[str, Any]:
        return {
            'student_code': self.student_code,
            'student_name': self.student_name,
            'cols': [col.to_dict() for col in self.cols]
        }


@dataclass
class Metadata:
    # 班级
    class_name: str
    # 行明细
    rows: List[Row]
    # 最后修改时间
    last_modified: str

    def to_dict(self) -> Dict[str, Any]:
        """返回可序列化的字典表示"""
        return {
            'class_name': self.class_name,
            'rows': [row.to_dict() for row in self.rows],
            'last_modified': self.last_modified
        }

    @property
    def __dict__(self) -> Dict[str, Any]:
        """返回可序列化的字典表示"""
        return self.to_dict()
